'use strict';

const Controller = require('egg').Controller;

class AdminController extends Controller {
    async admin() {
        const { ctx } = this;
        let result = ctx.request.body;
        console.log(result);
        ctx.body = {
            code: 1,
            userinfo: ctx.userInfo
        };
    }

    async adlogin() {
        const { ctx } = this;
        let res = { code: 1, msg: '登录成功' };
        // 接收提交上来的数据
        const data = ctx.request.body;
        // 去数据库拿数据对比
        const result = await this.app.mysql.get('admin', { tel: data.tel });
        if (!result) {
            res = { code: -1, msg: '手机号不存在' };
        } else if (result.passwd != data.passwd) {
            res = { code: 0, msg: '密码错误' };
        } else {
            // 登录成功后保存session信息
            // ctx.session.adminid = result.adminid;
            // ctx.session.tel = result.tel;
            // ctx.session.adname = result.adname;
            // token会话 登录成功后
            const token = ctx.app.jwt.sign({
                adminid: result.adminid,
                tel: result.tel,
                adname: result.adname,
                avater: result.avater
            }, this.app.config.jwt.secret);
            res.token = token;
        }
        ctx.body = res;
    }

    // 获取文章列表
    async list() {
        const { ctx } = this;
        let res = { "code": 0, "msg": "数据获取成功", "count": 0, "data": [] };
        //分页用到的页数及每页显示条数
        const page = ctx.request.query.page / 1 ? ctx.request.query.page / 1 : 1;
        const limit = ctx.request.query.limit / 1 ? ctx.request.query.limit / 1 : 20;
        //到数据表里面去验证数据的正确性
        const results = await this.app.mysql.select('articles', {
            where: { status: 1 }, // WHERE 条件  // adminid: ctx.userInfo.adminid
            columns: ['aid', 'title', 'tags', 'content', 'cover', 'uid', 'addtimes', 'comments', 'supports', 'collects'], // 要查询的表字段
            orders: [['aid', 'asc']], // 排序方式
            limit: limit, // 返回数据量
            offset: (page - 1) * limit, // 数据偏移量
        });
        res.data = results;
        //查询满足条件的记录数
        const sql = `SELECT count(aid) as nums FROM articles WHERE status = 1`; //AND uid = ${ctx.session.uid}
        const r = await this.app.mysql.query(sql);
        console.log(r); //获取满足条件的数据

        res.count = r[0].nums;
        ctx.body = res;
    }

    // 用户禁言
    async del() {
        const { ctx } = this;
        let res = { code: 1, msg: '操作成功' };
        //接收客户端提交上来的GET数据
        const id = ctx.request.query.id;
        console.log(id);
        const row = {
            //id, //如果主键不是id请看官方文档，或者直接写sql语句
            status: 0,
        };
        const options = {
            where: {
                uid: id
            }
        };
        const result = await this.app.mysql.update('users', row, options);
        console.log(result);
        if (!result.affectedRows) {
            res = { code: -1, msg: '操作失败' };
        }

        ctx.body = res;
    }
    // 用户解言
    async update() {
        const { ctx } = this;
        let res = { code: 1, msg: '操作成功' };
        //接收客户端提交上来的GET数据
        const id = ctx.request.query.id;
        const row = {
            // id, //如果主键不是id请看官方文档，或者直接写sql语句
            status: 1,
        };
        const options = {
            where: {
                uid: id
            }
        };
        const result = await this.app.mysql.update('users', row, options);
        console.log(result);
        if (!result.affectedRows) {
            res = { code: -1, msg: '操作失败' };
        }

        ctx.body = res;
    }
}
module.exports = AdminController;